/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities.ai;

import java.util.Random;
import rpg.entities.LivingEntity;

/**
 *
 * @author rothens
 */
public class AIPeacefulRandomMovement extends AI {

    Random r;
    private int nextRandom = 20;

    public AIPeacefulRandomMovement(LivingEntity entity) {
        super(entity);
        r = new Random();
    }

    @Override
    public boolean tick() {
        if(entity == null){
            return false;
        }

        animDelay--;
        if (animDelay <= 0) {
            --nextRandom;
            animDelay = 5;
            animState++;
            animState %= 3;

        }
        if (nextRandom <= 0) {
            nextRandom = 18 + r.nextInt(10);
            nextLoc = entity.getCurrentLocation().getPossibleLocation(entity.getMap());
        }
        
        int moveDir = nextLoc.getDir(entity.getCurrentLocation());

        if (moveDir != -1) {
            dir = moveDir;
            if (!entity.getCurrentLocation().move(moveDir, 1)) {
                nextLoc = entity.getCurrentLocation().getPossibleLocation(entity.getMap());
            }
        } else {
            if (r.nextBoolean()) {
                nextLoc = entity.getCurrentLocation().getPossibleLocation(entity.getMap());
            }
        }
        return entity != null && entity.getHealth()>0;
    }
}
